package cn.mediinfo.dianzidp.builder;

import cn.com.listentech.LEDControl;
import cn.com.listentech.ScreenInfo;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.StrUtil;

import cn.mediinfo.dianzidp.dto.YaoFangChuangKouXXDto;
import cn.mediinfo.dianzidp.dto.YaoFangXXDto;
import cn.mediinfo.dianzidp.util.SendUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.List;

@Slf4j
@Component
public class YaoFangLEDBuilder{

    // 会话句柄 对于当前连接来说 每次只需要一个会话
    public int gi_handle = 0;
    public int ii_count = 0;

    public String ls_ip;

    //todo 传过去需要㐰号
    public int ls_port = 10000;

    //controlType 型号。2是T系列，3是E，Q系列卡
    public int gi_cotrolType = 3;

    public int ii_flag_2 = 0;


    public void wf_send_dp1(YaoFangXXDto yaoFangXXDto) throws RuntimeException, ParseException {
        String ls_ip;
        String ls_value;
        String ls_value1;
        String ls_value2;
        Integer li_ret;
        Integer li_pno;
        Integer li_lenth_value1;
        Integer li_lenth_value2;
        String ls_space_value1;
        String ls_space_value2;
        Integer li_rowck0;
        Integer li_rowck1;
        String ls_tmp;
        String ls_space;
        String ls_head;
        String ls_brxm;
        Integer li_j;
        Integer li_y;
        Integer li_col;
        Integer li_sum;
        ls_ip = "192.168.17.160";
        li_pno = 1;
        if (gi_handle > 0) {
            li_ret = LEDControl.EndSend(gi_handle);
        }
        //1.启动会话
        gi_handle = LEDControl.StartSend();
        //2.设置通讯模式
        if (LEDControl.SetTransMode(gi_handle, 1, 0, gi_cotrolType, 1) == 0) {
            throw new RuntimeException("通讯模式设置失败!");
        }
        ls_ip = "192.168.17.159";
        if (LEDControl.SetNetworkPara(gi_handle, li_pno, ls_ip, ls_port) == 2) {
            ii_count++;
            throw new RuntimeException("设置通信参数失败");
        }

        ScreenInfo sc = new ScreenInfo();
        sc.screenwidth = 576;
        sc.screenheight = 160;
        sc.dbcolor = 1;
        sc.baud = 115200;
        sc.screenname = "";
        li_ret = LEDControl.SetScreenInfo(gi_handle, sc);
        li_ret = LEDControl.AddControl(gi_handle, li_pno, 2);
        li_ret = LEDControl.AddProgram(gi_handle, li_pno, 0);
        ls_head = SendUtil.getKongGeSpaces(3) + "中药窗" + "      " + "西药窗①" + SendUtil.getKongGeSpaces(7);
        LEDControl.AddQuitText(gi_handle, 1, li_pno, 0, 0, 576, 60, 255, "宋体", 30, true, false, false, ls_head);

        li_y = 50;
        /**
         * 此处需要查询li_row 的相关数据
         */
        List<YaoFangChuangKouXXDto> chuangkouxx_00 = yaoFangXXDto.getChuangkouxx_00();
        List<YaoFangChuangKouXXDto> chuangkouxx_01 = yaoFangXXDto.getChuangkouxx_01();

        for (int li_i = 0; li_i < 5; li_i++) {
            if (li_i > 1) {
                li_y = li_y + 20;
            }
            ls_value1 = handleBingRenXM(chuangkouxx_00, li_i, 8);
            ls_value2 = handleBingRenXM(chuangkouxx_01, li_i, 8);

            li_lenth_value1 = 32 - ls_value1.length();
            ls_space_value1 = SendUtil.getKongGeSpaces(li_lenth_value1);
            li_lenth_value2 = 30 - ls_value2.length();
            ls_space_value2 = SendUtil.getKongGeSpaces(li_lenth_value2);

            ls_value = StrUtil.builder().append(" ").append(ls_value1).append(ls_space_value1).append(" ").append(ls_value2).append(ls_space_value2).toString();
            LEDControl.AddQuitText(gi_handle, 1, li_pno, 0, li_y, 570, 31, 255, "宋体", 14, false, false, false, ls_value);
        }
        li_ret = LEDControl.SendControl(gi_handle, 1, 0);
        log.error("sendControl {}", li_ret);
        li_ret = LEDControl.EndSend(gi_handle);
        log.error("EndSend {}", li_ret);
    }


    /**
     * 药房屏幕发送相关  西药
     *
     * @param yaoFangXXDto
     * @throws RuntimeException
     * @throws ParseException
     */
    public void wf_send_dp2(YaoFangXXDto yaoFangXXDto) throws RuntimeException, ParseException {
        String ls_ip;
        String ls_value;
        String ls_value2;
        String ls_value3;
        String ls_value4;
        String ls_value5;
        Integer li_ret;
        Integer li_pno;
        Integer li_lenth_value2;
        Integer li_lenth_value3;
        Integer li_lenth_value4;
        Integer li_lenth_value5;
        String ls_space_value2;
        String ls_space_value3;
        String ls_space_value4;
        String ls_space_value5;
        String ls_head;
        Integer li_y;
        li_pno = 1;
        if (gi_handle > 0) {
            li_ret = LEDControl.EndSend(gi_handle);
        }
        //1.启动会话
        gi_handle = LEDControl.StartSend();
        //2.设置通讯模式
        if (LEDControl.SetTransMode(gi_handle, 1, 0, gi_cotrolType, 1) == 0) {
            throw new RuntimeException("通讯模式设置失败!");
        }
        ls_ip = "192.168.17.159";
        if (LEDControl.SetNetworkPara(gi_handle, li_pno, ls_ip, ls_port) == 2) {
            ii_count++;
            throw new RuntimeException("设置通信参数失败");
        }

        ScreenInfo sc = new ScreenInfo();
        sc.screenwidth = 1216;
        sc.screenheight = 160;
        sc.dbcolor = 1;
        sc.baud = 115200;
        sc.screenname = "";
        li_ret = LEDControl.SetScreenInfo(gi_handle, sc);
        li_ret = LEDControl.AddControl(gi_handle, li_pno, 2);
        li_ret = LEDControl.AddProgram(gi_handle, li_pno, 0);
        /**
         * 此处需要查询li_row 的相关数据
         */
        List<YaoFangChuangKouXXDto> chuangkouxx_02 = yaoFangXXDto.getChuangkouxx_02();
        List<YaoFangChuangKouXXDto> chuangkouxx_03 = yaoFangXXDto.getChuangkouxx_03();
        List<YaoFangChuangKouXXDto> chuangkouxx_04 = yaoFangXXDto.getChuangkouxx_04();
        List<YaoFangChuangKouXXDto> chuangkouxx_05 = yaoFangXXDto.getChuangkouxx_05();
//        li_rowck2 = chuangkouxx_02.size();
//        li_rowck3 = chuangkouxx_03.size();
//        li_rowck4 = chuangkouxx_04.size();
//        li_rowck5 = chuangkouxx_05.size();
//        ls_space = SendUtil.getKongGeSpaces(11);
        ls_head = SendUtil.getKongGeSpaces(4) + "西药窗②" + SendUtil.getKongGeSpaces(6) + "西药窗③" + SendUtil.getKongGeSpaces(6) + "西药窗④" + SendUtil.getKongGeSpaces(6) + "西药窗⑤" + SendUtil.getKongGeSpaces(4);
        LEDControl.AddQuitText(gi_handle, 1, li_pno, 0, 5, 1200, 60, 255, "宋体", 30, true, false, false, ls_head);
        li_y = 50;
        // 这是五行数据，静态文本推送
        for (int li_i = 0; li_i < 5; li_i++) {
            if (li_i > 1) {
                li_y = li_y + 20;
            }
            ls_value2 = handleBingRenXM(chuangkouxx_02, li_i, 10);
            ls_value3 = handleBingRenXM(chuangkouxx_03, li_i, 10);
            ls_value4 = handleBingRenXM(chuangkouxx_04, li_i, 10);
            ls_value5 = handleBingRenXM(chuangkouxx_05, li_i, 10);

            li_lenth_value2 = 34 - ls_value2.length();
            ls_space_value2 = SendUtil.getKongGeSpaces(li_lenth_value2);
            li_lenth_value3 = 34 - ls_value3.length();
            ls_space_value3 = SendUtil.getKongGeSpaces(li_lenth_value3);
            li_lenth_value4 = 34 - ls_value3.length();
            ls_space_value4 = SendUtil.getKongGeSpaces(li_lenth_value4);
            li_lenth_value5 = 34 - ls_value4.length();
            ls_space_value5 = SendUtil.getKongGeSpaces(li_lenth_value5);
            ls_value = StrUtil.builder().append(" ").append(ls_value2).append(ls_space_value2).append(ls_value3).append(ls_space_value3).append(ls_value4).append(ls_space_value4).append(ls_value5).append(ls_space_value5).toString();
            LEDControl.AddQuitText(gi_handle, 1, li_pno, 10, li_y, 1200, 31, 255, "新宋体", 14, false, false, false, ls_value);
        }
        li_ret = LEDControl.SendControl(gi_handle, 1, 0);
        log.error("sendControl {}", li_ret);
        li_ret = LEDControl.EndSend(gi_handle);
        log.error("EndSend {}", li_ret);
    }

    /**
     * 药房LED处理病人姓名方法
     *
     * @param yaoFangChuangKouXXDtos
     * @return
     */
    private static String handleBingRenXM(List<YaoFangChuangKouXXDto> yaoFangChuangKouXXDtos, int li, int jianGeChangDu) {
        String resValue = "";
        int limitStart = li * 2;
        int limitEnd = limitStart + 2;
        /**
         * subList form(包含) to(不包含)
         * 只取前两个
         * 使用ListUtil的sub 因为此方法会拷贝原对象,不会对原数据进行截取
         */
        List<YaoFangChuangKouXXDto> res = ListUtil.sub(yaoFangChuangKouXXDtos, limitStart, limitEnd);
        for (YaoFangChuangKouXXDto re : res) {
            String bingRengXM = re.getBingRenXM();
            if (StrUtil.isBlank(bingRengXM)) {
                bingRengXM = "";
            }
            int totalChangDu = jianGeChangDu - bingRengXM.length();
            if (totalChangDu > 0) {
                resValue = resValue + bingRengXM + SendUtil.getKongGeSpaces(totalChangDu);
            } else {
                resValue = resValue + SendUtil.getKongGeSpaces(jianGeChangDu);
            }
        }
        return resValue;
    }
}
